package com.sheepjin.design_pattern.创建型模型.工厂模式.intf;

import com.sheepjin.design_pattern.创建型模型.工厂模式.entity.Circle;
import com.sheepjin.design_pattern.创建型模型.工厂模式.entity.Diamond;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： SheepJin
 * @date： 2022/6/2 - 21:46
 * @description：工厂类
 * @modifiedBy：
 */
public interface ShapeFactory {


    enum SHAPEUTILS {

        CIRCLE(Circle.class),
        DIAMOND(Diamond.class);

        public Class shapeClass;

        SHAPEUTILS(Class shapeClass) {
            this.shapeClass = shapeClass;
        }

        /**
         * 通过形状编码获取指定形状的新类
         *
         * @param shapeClass shape接口的子类字节码类
         * @return 形状接口实例对象
         */
        public static Shape getShapeByShapeClass(Class shapeClass) {
            try {
                return (Shape) shapeClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    enum ShapeFactoryUtils {

        CIRCLE("CIRCLE", new Circle()),
        DIAMOND("DIAMOND", new Diamond());

        public String shapeCode;
        public Shape shape;
        public static Map<String, Shape> shapeLookup = new HashMap<>();

        ShapeFactoryUtils(String shape, Shape shapeClass) {
            this.shapeCode = shape;
            this.shape = shapeClass;
        }

        static {
            for (ShapeFactoryUtils shape : ShapeFactoryUtils.values()) {
                shapeLookup.put(shape.shapeCode, shape.shape);
            }
        }

        /**
         * 通过形状编码获取指定形状的新类
         *
         * @param shapeCode 形状编码
         * @return 指定形状类
         */
        public static Shape getShapeByShapeCode(String shapeCode) {
            return shapeLookup.get(shapeCode);
        }
    }
}
